using Admin2025.Domain.Entities.App;
using Admin2025.Infrastructure.Security;
using Microsoft.EntityFrameworkCore;

namespace Admin2025.Infrastructure.Data;

public class Admin2025DbContext : DbContext
{
    public Admin2025DbContext(DbContextOptions options) : base(options) { }

    public DbSet<AppUser> AppUser { get; set; }
    public DbSet<AppRole> AppRole { get; set; }
    public DbSet<AppPermission> AppPermission { get; set; }
    public DbSet<AppUserRole> AppUserRole { get; set; }
    public DbSet<AppRolePermission> AppRolePermission { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // 静态Id
        var adminUserId = Guid.Parse("11111111-1111-1111-1111-111111111111");
        var adminRoleId = Guid.Parse("22222222-2222-2222-2222-222222222222");
        var permUserManageId = Guid.Parse("33333333-3333-3333-3333-333333333333");
        var permRoleManageId = Guid.Parse("44444444-4444-4444-4444-444444444444");

        var defaultTime = new DateTime(2025, 7, 2, 0, 0, 0, DateTimeKind.Utc);
        // 权限
        modelBuilder.Entity<AppPermission>().HasData(
            new {
                Id = permUserManageId,
                Name = "User.Manage",
                Description = "用户管理",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            },
            new {
                Id = permRoleManageId,
                Name = "Role.Manage",
                Description = "角色管理",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            }
        );

        // 角色
        modelBuilder.Entity<AppRole>().HasData(
            new {
                Id = adminRoleId,
                Name = "Admin",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            }
        );

        // 用户
        modelBuilder.Entity<AppUser>().HasData(
            new {
                Id = adminUserId,
                UserName = "admin",
                Password="113",
                Salt="333",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            }
        );

        // 用户-角色
        modelBuilder.Entity<AppUserRole>().HasData(
            new {
                Id = Guid.NewGuid(),
                UserId = adminUserId,
                RoleId = adminRoleId,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            }
        );

        // 角色-权限
        modelBuilder.Entity<AppRolePermission>().HasData(
            new {
                Id = Guid.NewGuid(),
                RoleId = adminRoleId,
                PermissionId = permUserManageId,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            },
            new {
                Id = Guid.NewGuid(),
                RoleId = adminRoleId,
                PermissionId = permRoleManageId,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = ""
            }
        );
    }
}